package artid.com.arttogo.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.android.volley.VolleyError;

import java.util.ArrayList;
import java.util.List;

import artid.com.arttogo.R;
import artid.com.arttogo.activity.CollectionDetailPagerActivity;
import artid.com.arttogo.adapter.ArtistWorksPicture_Adapter;
import artid.com.arttogo.bean.NewartListParser;
import artid.com.arttogo.db.Cache;
import artid.com.arttogo.network.VolleyBasic;
import artid.com.arttogo.network.VolleyCallback;
import artid.com.arttogo.network.VolleyData;
import artid.com.arttogo.network.VolleyMap;
import artid.com.arttogo.utils.NetUtils;
import artid.com.arttogo.view.xlistview.PLA_AdapterView;
import artid.com.arttogo.view.xlistview.XListView;

/**
 * 项目名称：ArtworkArt
 * 类名称：CollecterDynActivity
 * 类描述：艺术家作品（瀑布流）
 * 创建人：耿卫斌
 * 创建时间：2017-1-10 上午11:16:47
 */
public class CollecterDynActivity extends BaseFragment implements XListView.IXListViewListener {
    private View mView;
    private Context mContext;
    private XListView mListView;
    private boolean isRefreshed = false;// 是否刷新
    private int page = 1;
    private ArtistWorksPicture_Adapter mAdapter;
    private List<NewartListParser> list;
    private List<NewartListParser> more_list;
    private LinearLayout noDataLL;//无数据布局
    private String imgwide;//请求的图片宽度
    private String artist_id;//艺术家ID
    ArrayList<String> listId;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        Bundle args = getArguments();
        artist_id = args != null ? args.getString("id") : "";
//		artist_id = "730";
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.activity_staggeredgrid, null);
        mContext = getActivity();
        initView(inflater);
        return mView;
    }

    private void initView(LayoutInflater inflater) {
        list = new ArrayList<NewartListParser>();
        more_list = new ArrayList<NewartListParser>();
        noDataLL = (LinearLayout) mView.findViewById(R.id.ll_hint);
        noDataLL.setVisibility(View.VISIBLE);
        imgwide = (Integer.parseInt(NetUtils.getDisplayMetrics((Activity) mContext).get(NetUtils.DM_WIDTH)) / 2) + "";
        mListView = (XListView) mView.findViewById(R.id.StaggeredGridActivity_listView);
        mListView.setPullRefreshEnable(true);
        mListView.setPullLoadEnable(true);
        mListView.setXListViewListener(this);
        listId = new ArrayList<String>();
        getData();
    }

    private void getData() {
        VolleyBasic.getInstance((Activity) mContext).post(Cache.newartList,
                VolleyMap.getInstance().newartList(page, 8, imgwide, artist_id),
                NewartListParser.class, new VolleyCallback() {

                    @Override
                    public void onSuccess(VolleyData vd) {
                        // TODO Auto-generated method stub
                        if (isRefreshed) {
                            mListView.stopRefresh();
                        } else {
                            mListView.stopLoadMore();
                        }
                        list = (List<NewartListParser>) vd.getList();
                        initentData(list);
                        if (isRefreshed == true) {
                            more_list.clear();
                            isRefreshed = false;
                        }
                        if (!list.isEmpty()) {
                            more_list.addAll(list);
                        }
                        //这里是显示有数据和无数据的布局
                        if (more_list != null && more_list.size() != 0) {
                            noDataLL.setVisibility(View.GONE);
                        } else {
                            noDataLL.setVisibility(View.VISIBLE);
                            return;
                        }
                        if (mAdapter == null) {
                            mAdapter = new ArtistWorksPicture_Adapter(mContext, more_list);
                            mListView.setAdapter(mAdapter);
                        }
                        mAdapter.notifyData(more_list);
                        Log.d("log", more_list.toString());
                    }

                    @Override
                    public void onError(VolleyError volleyError) {
                        // TODO Auto-generated method stub
                        if (isRefreshed) {
                            mListView.stopRefresh();
                        } else {
                            mListView.stopLoadMore();
                        }
                        //这里是显示有数据和无数据的布局
                        if (more_list != null && more_list.size() != 0) {
                            noDataLL.setVisibility(View.GONE);
                        } else {
                            noDataLL.setVisibility(View.VISIBLE);
                            return;
                        }
                    }
                });

        mListView.setOnItemClickListener(new PLA_AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(PLA_AdapterView<?> parent, View view,
                                    int position, long id) {
                // TODO Auto-generated method stub
                position = position - 1;
                Intent intent = new Intent(mContext, CollectionDetailPagerActivity.class);
                intent.putExtra("id", listId);
                intent.putExtra("index", position);
                intent.putExtra("titleName", more_list.get(position).getArtname());
                startActivity(intent);
            }
        });
    }


    private void initentData(List<NewartListParser> vd) {

        if (vd.isEmpty()) {
            return;
        }
        if (page == 1) {
            listId = new ArrayList<String>();

        }
        for (int i = 0; i < vd.size(); i++) {
            listId.add(vd.get(i).getArt_id());
        }

    }

    @Override
    public void onRefresh() {
        // TODO Auto-generated method stub
        //下拉刷新
        page = 1;
        isRefreshed = true;
        getData();
    }

    @Override
    public void onLoadMore() {
        // TODO Auto-generated method stub
        //上拉加载
        page++;
        getData();
    }

}
